package com.sylar.happyreader.adapter;

import java.util.List;

import android.app.Activity;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

import com.sylar.happyreader.R;
import com.sylar.happyreader.bean.BookItem;
import com.sylar.happyreader.tools.Config;
import com.sylar.happyreader.view.BookViewHolder;
import com.sylar.happyreader.view.RemoteImageView;

public class BookGridAdapter extends BaseAdapter {
	private Activity mContext;
	private List<BookItem> mBookArrayList;
 
	public BookGridAdapter(Activity context,List<BookItem> items) {
		super();
		mContext = context;
		mBookArrayList = items;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		BookViewHolder holder = null;
		if (convertView == null) {
			convertView = mContext.getLayoutInflater().inflate(R.layout.text_row1, parent, false);
			holder = new BookViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (BookViewHolder) convertView.getTag();
		}
		
		BookItem bookitem = getItem(position);
		
		if(bookitem!=null){
			String coverPath = Config.CoverUri + bookitem.getSex() + "/" + Uri.encode(bookitem.getStyle()) + "/" + Uri.encode(bookitem.getBookname()) + "/" + Uri.encode(bookitem.getBookname()) + ".jpg";

			TextView bookName = holder.getBookName();
			
			bookName.setText(bookitem.getBookname());

			RemoteImageView remoteImage = holder.getRemoteImage();
			remoteImage.setScaleType(ScaleType.FIT_XY);
			remoteImage.setDefaultImage(R.drawable.default_book_cover);
			remoteImage.setImageUrl(coverPath);
		}
		
		return convertView;
	}


	public int getCount() {
		if(mBookArrayList!=null)
			return mBookArrayList.size();
		return 0;
	}



	public BookItem getItem(int position) {
		if(mBookArrayList!=null && mBookArrayList.size()>position)
			return mBookArrayList.get(position);
		return null;
	}



	public long getItemId(int position) {
		return 0;
	}
}
